package gui;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;

import java.util.*;

import db.*;
import MoC.CompositeActorResult;
import MoC.ClassResult;

//////////////////////////////////////////////////////////////////////////
//// ResultsFrame
/**

@author Ivan Jeukens
@version v 0.1 11/07/2003
*/
public class ResultsFrame extends JFrame {

    public ResultsFrame(CompositeActorInfo top) {
        super();
        setTitle("Analysis Results");
        setSize(500,700);

        _top = top;

        JPanel main = new JPanel();
        main.setLayout(new BorderLayout());

        DefaultMutableTreeNode topnode = _createTree(top);

        JTree tree = new JTree(topnode);
        main.add(BorderLayout.CENTER, tree);

        /*
        String [] displayOptions = { "Possible Solutions",  "Atomic Actors"};
        final JComboBox displayOptionsBox = new JComboBox(displayOptions);
        displayOptionsBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String s = (String) displayOptionsBox.getSelectedItem();
                if(s.equals(_lastSelected)) return;

                _displayResult(s);
                _lastSelected = s;
            }

            private String _lastSelected = "";
        });
        main.add(displayOptionsBox, BorderLayout.NORTH);

        _textArea = new JTextArea();
        _textArea.setEditable(false);
        main.add(_textArea, BorderLayout.CENTER);
        */

        getContentPane().add(main);
    }


    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    ///////////////////////////////////////////////////////////////////
    ////                           private methods                 ////

    private DefaultMutableTreeNode _createTree(CompositeActorInfo info) {
    	StringBuffer buf = new StringBuffer(info.getName() + " : ");
    	CompositeActorResult ares = info.getResult();
        if(ares.isSpecific()) {
            buf.append("Specific to " + ares.getSpecificMoC());
        }
        else {
            buf.append("Directors for: ");
            Iterator ite = ares.associatedDirectors();
            while(ite.hasNext()) {
                buf.append((String) ite.next() + " ");
            }
        }
        DefaultMutableTreeNode ret = new DefaultMutableTreeNode(buf.toString());

    	Iterator ite = info.compositeActorsIterator();
    	while(ite.hasNext()) {
            CompositeActorInfo subinfo = (CompositeActorInfo) ite.next();
            DefaultMutableTreeNode subn = _createTree(subinfo);
            ret.add(subn);
    	}

        ite = info.atomicActorsIterator();
        while(ite.hasNext()) {
            AtomicActorInfo ainfo = (AtomicActorInfo) ite.next();
            buf = new StringBuffer(ainfo.getName() + " : ");
            ClassResult res = ainfo.getResult();

            if(res.isSpecific()) {
                buf.append("Specific to: " + res.getSpecificMoC());
            }
            else {
                Iterator vite = res.validForIterator();
                buf.append("Valid for: ");
                while(vite.hasNext()) {
                    buf.append((String)vite.next() + " ");
                }
            }
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(buf.toString());
            ret.add(node);
        }

    	return ret;
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private CompositeActorInfo _top;
}